import { Component } from '@angular/core';

import { ToastController, NavController, AlertController, Platform, ModalController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { SettingSealService } from './seal.service';
import { Config } from '../../../providers/config';

import { Common } from '../../../providers/common';

import { EmailDetailService } from '../../email/detail/detail.service';

import _ from "lodash";


@Component({
	selector: 'page-setting-seal',
	templateUrl: 'seal.html',
	providers: [SettingSealService, EmailDetailService]
})
export class SettingSealPage {
	moa_page_name: any = "page-setting-seal";
  moa_page_explain: any = "预留印鉴";

  sealContent = "";

	constructor(
		public nav: NavController,
		public setttingSealService: SettingSealService,
		public emailDetailService: EmailDetailService,
		public alertCtrl: AlertController,
		public platform: Platform,
		public config: Config,
		public toastCtrl: ToastController,
		public modalCtrl: ModalController,
		public common: Common,
		public storage: Storage
	) {
	  let that = this;
		this.storage.get('userInfo').then(function (userInfo) {
			if (userInfo.seal) {
				document.getElementById("seal-content")['value'] = userInfo.seal;
				that.sealContent = userInfo.seal;
			}
		});
	}

	seal() {
		let that = this;
		let content = this.sealContent;//document.getElementById("seal-content")['value'];
		/*if (content.trim().length == 0) {
			let toast = this.toastCtrl.create({
				message: '请填写你的预留印鉴',
				position: 'middle',
				duration: 2000,
				dismissOnPageChange: true
			});
			toast.present();
			return false;
		}*/

		this.setttingSealService.seal(content).subscribe(data => {
			if (data) {

				this.storage.get('userInfo').then(function (userInfo) {
					userInfo.seal = data.content;
					that.storage.set('userInfo', userInfo);
				});

        let message = "";
        if (this.sealContent) {
          message = "保存预留印鉴成功";
        } else {
          message = "删除预留印鉴成功";
        }
				let alert = this.alertCtrl.create({
					title: '提示',
					subTitle: message,
					"buttons": ['确认']
				});
				alert.present();
				this.nav.pop();
			}
		});

	}

  inputSealContent() {

	  this.sealContent = _.replace(this.sealContent, /\r\n/g, "");
	  this.sealContent = _.replace(this.sealContent, /\n/g, "");
	  this.sealContent = _.replace(this.sealContent, /\r/g, "");
  }
}
